package cn.imvc.app.controllers.goods;


import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import cn.imvc.app.entities.Brand;
import cn.imvc.app.entities.BrandGoodsClass;
import cn.imvc.app.entities.GoodsClass;
import cn.imvc.app.models.BrandGoodsClassModel;
import cn.imvc.app.models.BrandModel;
import cn.imvc.app.models.GoodsClassModel;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;
/**
 * 
 * @author zhy
 * @date 2016年8月4日下午2:34:51
 *
 */
public class BrandGoodsClassControllerImpl implements BrandGoodsClassController{
	static Logger logger = Logger.getLogger(BrandGoodsClassControllerImpl.class);
	public Page<BrandGoodsClass> page;
	public ViewRequest viewRequest;
	public BrandGoodsClass brandGoodsClass;
	public int id;
	private int[] ids;
	public Brand brand;
	public GoodsClass goodsClass;
	public int brandId;
	public int goodsClassId;
	
	@Autowired
	BrandGoodsClassModel brandGoodsClassModel;
	@Autowired
	GoodsClassModel goodsClassModel;
	@Autowired
	BrandModel brandModel;
	@Autowired
	LoggingModel loggingModel;
	public String search() {
	
		return null;
	}

	public String index() {
		
		Map<String, String> searchMap = new HashMap<String, String>();
		searchMap.put("goodsClassId_eq_string", String.valueOf(goodsClassId));
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.setParams(searchMap);
		page = brandGoodsClassModel.find(viewRequest);

		return "brand-goods-class/index";
	}

	public String show() {
		
		return "";
	}

	public String editNew() {
	
		goodsClass = goodsClassModel.get(goodsClassId);
		
		return "brand-goods-class/edit";
	}

	public String create() {
		brandGoodsClass.setGoodsClass(goodsClassModel.get(goodsClassId));
		brandGoodsClass.setBrand(brandModel.get(brand.getId()));
		int result = brandGoodsClassModel.save(goodsClassId,brandId,brandGoodsClass);
		if(result==-1){
			return Render.json(new DialogBean("300","该品牌已添加","brand_goods_class","closeCurrent"));
		}
		loggingModel.info(logger,"添加品牌类别："+brandGoodsClass.getBrandName());
		return Render.json(new DialogBean("200","添加成功","brand_goods_class","closeCurrent"));
	}

	public String edit() {
		brandGoodsClass = brandGoodsClassModel.get(id);
		goodsClass = brandGoodsClass.getGoodsClass();
		brand = brandGoodsClass.getBrand();
		return "brand-goods-class/edit";
	}

	public String update() {
		brandGoodsClass.setGoodsClass(goodsClassModel.get(goodsClassId));
		brandGoodsClass.setBrand(brandModel.get(brand.getId()));
		int result = brandGoodsClassModel.update(id,goodsClassId,brandId,brandGoodsClass);
		if(result==-1){
			return Render.json(new DialogBean("300","该品牌已添加","brand_goods_class","closeCurrent"));
		}
		loggingModel.info(logger,"修改品牌类别："+brandGoodsClass.getBrandName());
		return Render.json(new DialogBean("200","修改成功","brand_goods_class","closeCurrent"));
	}

	public String destroy() {
		brandGoodsClassModel.delete(id);
		loggingModel.info(logger,"删除品牌类别："+id);
		return Render.json(new DialogBean("200","删除成功","brand_goods_class"));
	}

	public String destroyBatch() {
		brandGoodsClassModel.deleteBatch(ids);
		return Render.json(new DialogBean("200","删除成功","brand_goods_class"));
	}
/**
 * 外部调用(首页内容)
 * 
 * @return
 */
	public String dialog() {
		
		Map<String, String> searchMap = new HashMap<String, String>();
		searchMap.put("goodsClassId_eq_string", String.valueOf(goodsClassId));
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.setParams(searchMap);
		page = brandGoodsClassModel.find(viewRequest);
		return "brand-goods-class/dialog";
	}
	/**
	 * 首页调用
	 * @return
	 */
  public String dialog2() {
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		page = brandGoodsClassModel.find(viewRequest);
		return "brand-goods-class/dialog2";
	}
	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public BrandGoodsClass getBrandGoodsClass() {
		return brandGoodsClass;
	}

	public void setBrandGoodsClass(BrandGoodsClass brandGoodsClass) {
		this.brandGoodsClass = brandGoodsClass;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int[] getIds() {
		return ids;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}

	public Brand getBrand() {
		return brand;
	}

	public void setBrand(Brand brand) {
		this.brand = brand;
	}

	public int getBrandId() {
		return brandId;
	}

	public void setBrandId(int brandId) {
		this.brandId = brandId;
	}

	public int getGoodsClassId() {
		return goodsClassId;
	}

	public void setGoodsClassId(int goodsClassId) {
		this.goodsClassId = goodsClassId;
	}

	public GoodsClass getGoodsClass() {
		return goodsClass;
	}

	public void setGoodsClass(GoodsClass goodsClass) {
		this.goodsClass = goodsClass;
	}

}
